#!/usr/bin/env python

from threading import Thread
from zmq_socket import ZmqServerPub,ZmqServerRep
from nor_socket import Server
from map_table import MapTable
from time import sleep


class ZmqServerPubWrapper(Thread):
    def __init__(self,port):
        super(ZmqServerPubWrapper, self).__init__()
        self.server_pub_ = ZmqServerPub(port=port)

    def run(self):
        while True:
            sleep(2)
            heartbeat_msg = "arm heartbeat.."
            print("Server > {0}".format(heartbeat_msg))
            self.server_pub_.send(heartbeat_msg)


class ZmqServerRepWrapper(Thread):
    def __init__(self,port):
        super(ZmqServerRepWrapper, self).__init__()
        self.server_rep_ = ZmqServerRep(port=port)
        self.map_table_ = MapTable()

    def run(self):
        while True:
            try:
                requ_str = self.server_rep_.recv()
                print("Recv: {0}".format(requ_str))
                resp_str = self.map_table_.getResponseString(requ_str)
            except Exception as e:
                print("Error: {0}".format(str(e)))
                resp_str = ""
            finally:
                self.server_rep_.send(resp_str)


class ServerWrapper(Thread):
    def __init__(self,port):
        super(ServerWrapper, self).__init__()
        self.server_ = Server(port=port)
        self.map_table_ = MapTable()
	self.kill_received_ = False
	self.setDaemon(True)

    def exit_thread(self):
        self.kill_received_ = True

    def run(self):
        while not self.kill_received_:
            print("waiting for a connection...")
            self.server_.accept()
            try:
                print("client connected.")
                while not self.kill_received_:
                    requ_str = self.server_.recv()
                    if requ_str=="":
                        break
                    print("Recv from TE > {0}".format(requ_str))
                    resp_str = self.map_table_.getResponseString(requ_str)
                    print("Send to TE > {0}".format(resp_str))
                    self.server_.send(resp_str)
            finally:
                self.server_.close_client()

    # def run(self):
    #     self.server_.accept()
    #     connect_flag = True
    #     while True:
    #         if not connect_flag:
    #             self.server_.accept()
    #         try:
    #             requ_str = self.server_.recv()
    #             print("Recv from TE > {0}".format(requ_str))
    #             #resp_str = self.map_table_.getResponseString(requ_str)
    #             resp_str = "xxxxx"
    #         except Exception as e:
    #             print("Error: {0}".format(str(e)))
    #             resp_str = "err"
    #             connect_flag = False
    #         finally:
    #             print("Send to TE > {0}".format(resp_str))
    #             self.server_.send(resp_str)




class DummyControlBoard(object):
    def __init__(self):
        super(DummyControlBoard, self).__init__()
        #self.arm_pub_thread_ = ZmqServerPubWrapper(port="6990")
        #self.arm_rep_thread_ = ZmqServerRepWrapper(port="7970")
        #self.fixture_pub_thread_ = ZmqServerPubWrapper(port="6470")
        #self.fixture_rep_thread_ = ZmqServerRepWrapper(port="6490")
        self.fixture_thread_ = ServerWrapper(port=7600)

    def run(self):
        print("Begin to launch arm&fixture service thread...")
        #self.arm_pub_thread_.start()
        #self.arm_rep_thread_.start()
        #self.fixture_pub_thread_.start()
        #self.fixture_rep_thread_.start()
        self.fixture_thread_.start()
        print("arm&fixture thread finish.")
        while True:
            sleep(1)
	#while False:
        #    try:
        #        sleep(1)
        #    except KeyboardInterrupt:
        #        self.fixture_thread_.exit_thread()
        #        break
        #self.fixture_thread_.join()
        #while True: pass


if __name__=="__main__":
    print("Begin dummy control board...")
    #pdb.set_trace()
    try:
        con_board = DummyControlBoard()
        con_board.run()
    except Exception as e:
        print("Error: {0}".format(str(e)))
 
    print("exit dummy control board.")
